package com.authine.cloudpivot.ext.applicationservice.report;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.model.ReportRecord;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.cloudpivot.ext.utils.system.ReportUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName PlatformReportService
 * @Description: 平台报价导出
 * @Author fj
 * @Date 2021/9/15
 * @Version V1.0
 **/
@Path("PlatformReportService")
@Slf4j
public class PlatformReportService {
    /**
     * 平台报价-询价商品列表-导出
     *
     * @param record 报表导出数据
     * @return 导出
     */
    @Path("platformQuotationGoodsReport")
    @POST
    @Operation(summary = "台报价-询价商品列表-导出", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> platformQuotationGoodsReport(@Parameter ReportRecord record) {
        if (StringUtils.isEmpty(record.getType())) {
            throw new BizException("报表导出类型不正确");
        }
        if (StringUtils.isEmpty(record.getCode())) {
            throw new BizException("报表导出文件不正确");
        }
        List<Map<String, Object>> reportList = Lists.newArrayList();// 报表数据
        // 通过询价商品id获取平台报价询价商品列表
        SingleResponse<BOList> vendorMatchedInquiryGoodsBOList = BoServiceUtils.findListByIn(ModelConst.T_VENDOR_MATCHED_INQ_GOODS, "inquiry_goods_id", record.getIds());
        log.info("-----------------------通过询价商品id获取平台报价询价商品列表：{}------------------------", JSONObject.toJSONString(vendorMatchedInquiryGoodsBOList));
        if (vendorMatchedInquiryGoodsBOList.getData().getTotal() > 0) {
            Map<String, Object> dataMap;
            for (Map<String, Object> goodsMap : vendorMatchedInquiryGoodsBOList.getData().getData()) {
                dataMap = Maps.newHashMap();
                dataMap.put("inquiryId", String.valueOf(goodsMap.get("inquiry_id")));
                dataMap.put("inquiryGoodsId", String.valueOf(goodsMap.get("inquiry_goods_id")));
                dataMap.put("brand", String.valueOf(goodsMap.get("brand")));
                dataMap.put("name", String.valueOf(goodsMap.get("name")));
                dataMap.put("model", String.valueOf(goodsMap.get("model")));
                dataMap.put("spec", goodsMap.get("spec") == null ? "" : String.valueOf(goodsMap.get("spec")));
                dataMap.put("unit", goodsMap.get("unit") == null ? "" : String.valueOf(goodsMap.get("unit")));
                dataMap.put("inquiryName", goodsMap.get("name") == null ? "" : String.valueOf(goodsMap.get("name")));
                dataMap.put("delivery", goodsMap.get("delivery") == null ? null : ((BigDecimal) goodsMap.get("delivery")).intValue());
                dataMap.put("num", goodsMap.get("num") == null ? 0d : ((BigDecimal) goodsMap.get("num")).doubleValue());
                dataMap.put("address", goodsMap.get("address") == null ? "" : String.valueOf(goodsMap.get("address")));
                dataMap.put("platformCategoryName", goodsMap.get("platform_category_name") == null ? "" : String.valueOf(goodsMap.get("platform_category_name")));
                dataMap.put("organizationCompanyName", goodsMap.get("organization_company_name") == null ? "" : String.valueOf(goodsMap.get("organization_company_name")));
                dataMap.put("departmentName", goodsMap.get("department_name") == null ? "" : String.valueOf(goodsMap.get("department_name")));
                dataMap.put("time", String.valueOf(goodsMap.get("createdTime")));
                dataMap.put("expire", String.valueOf(goodsMap.get("expire")));
                dataMap.put("isMatching", "1".equals(String.valueOf(goodsMap.get("is_auto_matching"))));
                dataMap.put("ifPricing", "1".equals(String.valueOf(goodsMap.get("is_quoted"))));
                reportList.add(dataMap);
            }
        }
        return ReportUtils.exportReport(record.getCode(), record.getType(), reportList);
    }
}
